package com.shopping.controller;

import com.shopping.common.Result;
import com.shopping.dto.ProductCommentDTO;
import com.shopping.entity.ProductComment;
import com.shopping.service.ProductCommentService;
import com.shopping.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/comments")
public class ProductCommentController {

    @Autowired
    private ProductCommentService productCommentService;

    @GetMapping("/product/{productId}")
    public Result<List<ProductCommentDTO>> getProductComments(@PathVariable Long productId) {
        try {
            List<ProductCommentDTO> comments = productCommentService.getProductComments(productId);
            return Result.success(comments);
        } catch (Exception e) {
            log.error("获取商品评论列表失败: productId={}, error={}", productId, e.getMessage());
            return Result.error("获取评论失败");
        }
    }

    @PostMapping
    @PreAuthorize("isAuthenticated()")
    public Result<Void> addComment(@RequestBody ProductCommentDTO commentDTO) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            if (currentUserId == null) {
                return Result.error("请先登录");
            }

            commentDTO.setUserId(currentUserId);
            productCommentService.addComment(commentDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("添加评论失败: userId={}, productId={}, error={}",
                    SecurityUtils.getCurrentUserId(), commentDTO.getProductId(), e.getMessage());
            return Result.error("添加评论失败");
        }
    }

    @DeleteMapping("/{commentId}")
    @PreAuthorize("isAuthenticated()")
    public Result<Void> deleteComment(@PathVariable Long commentId) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            if (currentUserId == null) {
                return Result.error("请先登录");
            }

            productCommentService.deleteComment(commentId, currentUserId);
            return Result.success();
        } catch (Exception e) {
            log.error("删除评论失败: userId={}, commentId={}, error={}",
                    SecurityUtils.getCurrentUserId(), commentId, e.getMessage());
            return Result.error("删除评论失败");
        }
    }

    @GetMapping("/product/{productId}/rating")
    public Result<Double> getProductRating(@PathVariable Long productId) {
        try {
            Double rating = productCommentService.getProductRating(productId);
            return Result.success(rating);
        } catch (Exception e) {
            log.error("获取商品评分失败: productId={}, error={}", productId, e.getMessage());
            return Result.error("获取评分失败");
        }
    }

    @GetMapping("/user/{userId}/product/{productId}")
    public Result<ProductCommentDTO> getUserProductComment(
            @PathVariable Long userId,
            @PathVariable Long productId) {
        return Result.success(productCommentService.getUserProductComment(userId, productId));
    }

    @GetMapping("/user")
    @PreAuthorize("isAuthenticated()")
    public Result<List<ProductCommentDTO>> getUserComments() {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            if (currentUserId == null) {
                return Result.error("请先登录");
            }

            List<ProductCommentDTO> comments = productCommentService.getUserComments(currentUserId);
            return Result.success(comments);
        } catch (Exception e) {
            log.error("获取用户评论列表失败: userId={}, error={}",
                    SecurityUtils.getCurrentUserId(), e.getMessage());
            return Result.error("获取评论列表失败");
        }
    }

    @PutMapping("/{commentId}")
    @PreAuthorize("isAuthenticated()")
    public Result<Void> updateComment(@PathVariable Long commentId, @RequestBody ProductCommentDTO commentDTO) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            if (currentUserId == null) {
                return Result.error("请先登录");
            }

            commentDTO.setId(commentId);
            commentDTO.setUserId(currentUserId);

            productCommentService.updateComment(commentDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("更新评论失败: userId={}, commentId={}, error={}",
                    SecurityUtils.getCurrentUserId(), commentId, e.getMessage());
            return Result.error("更新评论失败");
        }
    }
}